package com.agileai.pm.module.test.exteral;

import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataRow;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.pm.cxmodule.ProjectDefectStatisticsManage;

public class ProjDefectStatisticsImpl extends BaseRestService implements ProjDefectStatistics{

	public String findProjTestScheme(String projectId) {
		String result = "";
		try {
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> testSchemeNameRecords = getService.testSchemeNameRecords(projectId);
			JSONArray jsonArray = new JSONArray();
			for(int i = 0; i < testSchemeNameRecords.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = testSchemeNameRecords.get(i);
				String testSchemeId = row.stringValue("PT_ID");
				String testSchemeName = row.stringValue("PT_NAME");
				jsonObject.put("testSchemeId", testSchemeId);
				jsonObject.put("testSchemeName", testSchemeName);
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		} catch (JSONException e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
	} 
	
	public String findProjProblemStatistics(String projectId,String testSchemeId) {
		String result = "";
		try {
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> projectDefectRecords = getService.findProjectDefectRecords(testSchemeId,projectId);
			JSONArray jsonArray = new JSONArray();
			for(int i = 0; i < projectDefectRecords.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = projectDefectRecords.get(i);
				String testSchemeName = row.stringValue("PF_NAME");
				String testSchemeProblem = row.stringValue("PF_DEFECTNUM");
				jsonObject.put("testSchemeName", testSchemeName);
				jsonObject.put("testSchemeProblem", testSchemeProblem);
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		} catch (JSONException e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
	} 
	
	public String findProjEliminatRateStatistics(String projectId,String testSchemeId) {
		String result = "";
		try {
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> eliminatRateRecords = getService.findEliminatRateRecords(testSchemeId,projectId);
			JSONArray jsonArray = new JSONArray();
			for(int i = 0; i < eliminatRateRecords.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = eliminatRateRecords.get(i);
				String testSchemeName = row.stringValue("PF_NAME");
				String defectNum = row.stringValue("PF_DEFECTNUM");
				if("0".equals(defectNum)){
					jsonObject.put("testSchemeName", testSchemeName);
					jsonObject.put("testSchemeProblem", "0");
				}else{
					double eliminatRate = Double.parseDouble(row.stringValue("PF_COMPLETENUM"))/Double.parseDouble(row.stringValue("PF_DEFECTNUM"));
					jsonObject.put("testSchemeName", testSchemeName);
					jsonObject.put("testSchemeProblem", eliminatRate);
				}
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		} catch (JSONException e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
	} 
	
	public String findProjFormDataStatistics(String projectId,String testSchemeId) {
		String result = "";
		try {
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> formDataRecords = getService.findFormDataRecords(testSchemeId,projectId);
			JSONObject jsonObject = new JSONObject();
			int sumTotalQuantity = 0;
			int sumCompleteNum = 0;
			int sumNoCompleteNum = 0;
			double sumEliminatRate = 0;
			int average = 0;
			int sumModifyNum = 0;
			for(int i = 0; i < formDataRecords.size(); i++){
				DataRow row = formDataRecords.get(i);
				String totalQuantity = row.stringValue("PF_DEFECTNUM");//总数量
				String completeNum = row.stringValue("PF_COMPLETENUM");//已完成数量
				String noCompleteNum = row.stringValue("PF_NOCOMPLETENUM");//未完成数量
				
				sumTotalQuantity += Integer.parseInt(totalQuantity);//总数量
				sumCompleteNum += Integer.parseInt(completeNum);//已完成数量
				sumNoCompleteNum += Integer.parseInt(noCompleteNum);//未完成数量
				
				String defectNum = row.stringValue("PF_DEFECTNUM");
				if("0".equals(defectNum)){  //消缺率
					jsonObject.put("eliminatRate", "0");
				}else{
					double formEliminatRate = Double.parseDouble(row.stringValue("PF_COMPLETENUM"))/Double.parseDouble(defectNum);
					average ++;
					sumEliminatRate += formEliminatRate;//消缺率
				}
				String modifyNum = row.stringValue("PF_NOCOMPLETENUM");//修改次数
				sumModifyNum += Integer.parseInt(modifyNum);//修改次数
			}
			
			double averageEliminatRate = sumEliminatRate/average*100;
			jsonObject.put("averageEliminatRate", averageEliminatRate);
			
			jsonObject.put("sumTotalQuantity", sumTotalQuantity);
			jsonObject.put("sumCompleteNum", sumCompleteNum);
			jsonObject.put("sumNoCompleteNum", sumNoCompleteNum);
			jsonObject.put("sumModifyNum", sumModifyNum);
			
			result = jsonObject.toString();
		} catch (JSONException e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return result;
	}
	
	public String findEliminatRateForm(String projectId,String testSchemeId){
		String result = "";
		try{
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> eliminatRateForm = getService.eliminatRateForm(projectId,testSchemeId);
			JSONArray jsonArray = new JSONArray();
			for(int i = 0; i < eliminatRateForm.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = eliminatRateForm.get(i);
				String testSchemeName = row.stringValue("PF_NAME");//功能名称
				String totalQuantity = row.stringValue("PF_DEFECTNUM");//总数量
				String completeNum = row.stringValue("PF_COMPLETENUM");//已完成数量
				String noCompleteNum = row.stringValue("PF_NOCOMPLETENUM");//未完成数量
				String modifyNum = row.stringValue("PF_NOCOMPLETENUM");//修改次数
				String charge = row.stringValue("PF_CHARGE");//负责人
				String defectNum = row.stringValue("PF_DEFECTNUM");
				if("0".equals(defectNum)){
					jsonObject.put("eliminatRate", "0");
				}else{
					double eliminatRate = Double.parseDouble(row.stringValue("PF_COMPLETENUM"))/Double.parseDouble(row.stringValue("PF_DEFECTNUM"))*100;
					jsonObject.put("eliminatRate", eliminatRate);
				}
				jsonObject.put("testSchemeName", testSchemeName);//功能名称
				jsonObject.put("totalQuantity", totalQuantity);//总数量
				jsonObject.put("completeNum", completeNum);
				jsonObject.put("noCompleteNum", noCompleteNum);
				jsonObject.put("modifyNum", modifyNum);
				jsonObject.put("charge", charge);
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		}catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		return result;
	}
	
	public String findProblemNumForm(String projectId,String testSchemeId){
		String result = "";
		try{
			ProjectDefectStatisticsManage getService = this.lookupService(ProjectDefectStatisticsManage.class);
			List<DataRow> problemNumForm = getService.problemNumForm(projectId,testSchemeId);
			JSONArray jsonArray = new JSONArray();
			for(int i = 0; i < problemNumForm.size(); i++){
				JSONObject jsonObject = new JSONObject();
				DataRow row = problemNumForm.get(i);
				String testSchemeName = row.stringValue("PF_NAME");//功能名称 
				String totalQuantity = row.stringValue("PF_DEFECTNUM");//总数量
				String completeNum = row.stringValue("PF_COMPLETENUM");//已完成数量
				String noCompleteNum = row.stringValue("PF_NOCOMPLETENUM");//未完成数量
				String modifyNum = row.stringValue("PF_NOCOMPLETENUM");//修改次数
				String testSchemeProblem = row.stringValue("PF_DEFECTNUM");//问题数
				String charge = row.stringValue("PF_CHARGE");//负责人
				jsonObject.put("testSchemeName", testSchemeName);//功能名称
				jsonObject.put("totalQuantity", totalQuantity);//总数量
				jsonObject.put("completeNum", completeNum);
				jsonObject.put("noCompleteNum", noCompleteNum);
				jsonObject.put("modifyNum", modifyNum);
				jsonObject.put("testSchemeProblem", testSchemeProblem);
				jsonObject.put("charge", charge);
				jsonArray.put(jsonObject);
			}
			result = jsonArray.toString();
		}catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		return result;
	}
}